package com.krain.ddbb.activity;

import android.content.Intent;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.krain.corelibrary.widget.TimerButton;
import com.krain.ddbb.R;
import com.krain.ddbb.base.BaseActivity;
import com.krain.ddbb.constant.Constant;
import com.krain.ddbb.entity.AppApi;
import com.krain.ddbb.entity.PersonDetailBean;
import com.krain.ddbb.entity.UserInfoBean;
import com.orhanobut.logger.Logger;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Background;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.Extra;
import org.androidannotations.annotations.ViewById;

import java.util.HashSet;
import java.util.Set;

import cn.jpush.android.api.JPushInterface;
import cn.jpush.android.api.TagAliasCallback;


@EActivity(R.layout.activity_login)
public class LoginActivity extends BaseActivity implements TimerButton.OnTimerListener {

    @ViewById(R.id.act_login_et_phone)
    EditText mEtPhone;//手机号
    @ViewById(R.id.act_login_et_code)
    EditText mEtAuthCode;//验证码
    @ViewById(R.id.act_login_tb_code)
    TimerButton mTvAuthCode;//获取验证码
    @ViewById(R.id.act_login_btn_submit)
    Button mBtnLogin;//登录
    @Extra("cls")
    Class cls;

    @AfterViews
    void afterView() {
        mEtPhone.addTextChangedListener(mPhoneTextWatcher);
        mTvAuthCode.setOnTimerListener(this);
    }

    @Click(R.id.act_login_btn_submit)
    void clickToLogin() {
        if (verifyConditions()) {
            showProgressDialog();
            Login();
        }
    }

    @Background
    void Login(){
        UserInfoBean userbean = AppApi.getsInstance(mContext).login(mEtPhone.getText().toString(), mEtAuthCode.getText().toString());
        if (userbean != null) {
            app.setmUserinfo(userbean);
            baseUtil.showToast(mContext,"登录成功！", Gravity.CENTER);
            app.setJpushTags(userbean);
            if (cls != null){
                Intent intent = new Intent(mContext,cls);
                startActivity(intent);
            }else {
                setResult(RESULT_OK);
                finish();
            }

        }
        dismissProgressDialog();
    }



    @Background
    void getCode(){
        AppApi.getsInstance(mContext).getAuthCode(mEtPhone.getText().toString());
    }

    /**
     * 检查输入
     *
     * @return
     */
    private boolean verifyConditions() {
        if (TextUtils.isEmpty(mEtPhone.getText())) {
            baseUtil.showSnackBar(this, "请填写手机号码！");
            return false;
        }
        if (TextUtils.isEmpty(mEtAuthCode.getText())) {
            baseUtil.showSnackBar(this, "请填写验证码！");
            return false;
        }
        if (!TextUtils.isEmpty(mEtPhone.getText())) {
            if (mContext.isChinesePhoneNumber(mEtPhone.getText().toString())) {
                return true;
            } else {
                baseUtil.showSnackBar(this, "请输入正确的手机号码!");
            }
            return false;
        }
        return true;
    }


    TextWatcher mPhoneTextWatcher = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {
            if (mContext.validateMobile(s.toString())) {
                mTvAuthCode.setEnabled(true);
                mTvAuthCode.setTextColor(getResources().getColor(R.color.house_detail_line_grey));
            } else {
                mTvAuthCode.setTextColor(getResources().getColor(R.color.get_code_blue));
                mTvAuthCode.setEnabled(false);
            }
            buttonEnableCheck();
        }
    };

    /**
     * 检查按钮是否可点击
     */
    void buttonEnableCheck() {
        if (mTvAuthCode.isCanStart() && mContext.validateMobile(mEtPhone.getText().toString())) {
            mTvAuthCode.setEnabled(true);
            mTvAuthCode.setTextColor(getResources().getColor(R.color.get_code_blue));
        } else {
            mTvAuthCode.setEnabled(false);
            mTvAuthCode.setTextColor(getResources().getColor(R.color.house_detail_line_grey));
        }
    }

    @Override
    protected String getToolbarTitle() {
        return "登录";
    }

    public static void jumpTothisActivity(BaseActivity activity) {
        Intent intent = new Intent(activity, LoginActivity_.class);
        activity.startActivity(intent);
    }

    public static void jumpTothisActivity(BaseActivity activity, Class<?> cls) {
        Intent intent = new Intent(activity, LoginActivity_.class);
        intent.putExtra("cls",cls);
        activity.startActivity(intent);
    }

    public static void jumpTothisActivity(BaseActivity activity,int requestCode) {
        Intent intent = new Intent(activity, LoginActivity_.class);
        activity.startActivityForResult(intent,requestCode);
    }

    @Override
    public void onClick() {
        getCode();
    }

    @Override
    public void onTiming(int remainTime) {

    }

    @Override
    public void onFinish() {
        buttonEnableCheck();
    }
}
